#!/usr/bin/python
# -*- coding: utf-8 -*-
# @name             : Redis配置
# @author           : OneYoung
# @create           : 2024/02/04
# @version          : v1.0
# @desc             :

import redis
from application.settings import REDIS_DB_URL


class RedisGetter:
    @staticmethod
    def redis_connect():
        """创建REDIS连接"""
        return redis.from_url(
            url=REDIS_DB_URL,
            decode_responses=True,
            health_check_interval=1
        )

    @classmethod
    def get(cls, key):
        """Redis获取数据"""
        rd = cls.redis_connect()
        value = rd.get(key)
        rd.close()
        return value

    @classmethod
    def mget(cls, keys, *args):
        """Redis获取多个数据"""
        rd = cls.redis_connect()
        value = rd.mget(keys, *args)
        rd.close()
        return value

    @classmethod
    def set(cls, key, value, *args, **kwargs):
        """Redis保存数据"""
        rd = cls.redis_connect()
        rd.set(key, value, *args, **kwargs)
        rd.close()

    @classmethod
    def mset(cls, args, **kwargs):
        """Redis保存多个数据"""
        rd = cls.redis_connect()
        rd.mset(args, **kwargs)
        rd.close()
